<?php

namespace Trawind\Tenancy\Models;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Builder;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;

/**
 * Class Tenant
 *
 * @package TrawindCloud\Models
 */
class Tenant extends BaseTenant implements TenantWithDatabase
{

    use HasDatabase, HasDomains;

    /**
     * @var string
     */
    protected $table = 'tenant_connections';

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function domains()
    {
        return $this->hasMany(config('tenancy.domain_model'), 'tenant_connection_id', 'id');
    }

    /**
     * @param $value
     * @return mixed
     */
    public function getTenancyDbPasswordAttribute($value)
    {
        return _decrypt($value);
    }

    /**
     * @return Builder
     */
    public function newQuery()
    {
        return $this->tenantQuery(parent::newQuery());
    }


    /**
     * @param $newQuery
     * @return Builder
     */
    public function tenantQuery(Builder $newQuery): Builder
    {
        return $newQuery->where(['microservices_id' => config('app.name')]);
    }

}
